package tothreedesignmodel.prototype;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @Author lzhphantom
 * @Description
 * @Date 创建于 10:37 2020/7/20
 * @Modified
 */
public class ShallowCloneProblem {
    public static void main(String[] args) throws CloneNotSupportedException {
        Student student = new Student(20,"zs");
        Teacher teacher = new Teacher("ls",student);
        Teacher o = (Teacher) teacher.clone();
        o.setName("ww");
        o.getStudent().setName("emz");
        o.getStudent().setAge(21);
        Teacher o1 = (Teacher) o.clone();
        System.out.println(teacher);
        System.out.println(o);
        //o1的student对象依然复制的是Teacher的student对象值
        System.out.println(o1);
    }
}

@Data
@AllArgsConstructor
class Teacher implements Cloneable{
    private String name;
    private Student student;
    @Override
    public Object clone() throws CloneNotSupportedException {
        // return super.clone();
        Teacher teacher = (Teacher) super.clone();
        teacher.setStudent((Student) teacher.getStudent().clone());
        return teacher;
    }
}
@Data
@AllArgsConstructor
class Student implements Cloneable{
    private  int age;
    private String name;
    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
